package com.lib.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 13:20
 * Created by CaoXiang on 2017/9/21.
 * Describe:观察者管理类
 */

public class ObserverManager implements SubjectListener {

    private static ObserverManager observerManager;
    //观察者接口集合
    private List<ObserverListener> list = new ArrayList<>();

    public static ObserverManager getInstance() {
        if (null == observerManager) {
            synchronized (ObserverManager.class) {
                if (null == observerManager) {
                    observerManager = new ObserverManager();
                }
            }
        }
        return observerManager;
    }


    /**
     * 加入队列监听
     *
     * @param observerListener
     */
    @Override
    public void add(ObserverListener observerListener) {
        list.add(observerListener);
    }

    /**
     * 通知观察者刷新数据
     *
     * @param content
     */
    @Override
    public void notifyObserver(String content) {
        for (ObserverListener observerListener : list) {
            observerListener.observerUpData(content);
        }
    }


    /**
     * 移除队列监听
     *
     * @param observerListener
     */
    @Override
    public void remove(ObserverListener observerListener) {
        if (list.contains(observerListener)) {
            list.remove(observerListener);
        }
    }
}
